package praktikum_I.blatt_I.aufgabe2;


import org.junit.Test;
import praktikum_I.blatt_I.aufgabe1.Mittelwert_Varianz;
import praktikum_I.blatt_I.aufgabe1.Mittelwert_Varianz_inter;
import praktikum_I.blatt_II.aufgabe1.Matrix_impl;
import praktikum_I.blatt_II.aufgabe1.Matrix_inter;

import static org.junit.Assert.*;

import java.util.*;

public class List_implTest  {

    private  List_inter toTest = new List_impl();

    @org.junit.Test
    public void testCons() throws Exception {
        List normal_list = new ArrayList(Arrays.asList("hallo", "du", "Wie", "gehts"));

        List_inter self_list = new List_impl();
        self_list.add("hallo");
        self_list.add("was");
        self_list.add("wie");
        self_list.add("gehts");



        self_list.cons("Stephan") ;

        assertEquals("Cons Fehler by Self!", "Stephan", self_list.getHead());

        System.out.println("========AddAll===========");
        System.out.println(""+ self_list);
        System.out.println("===================");


    }

    @org.junit.Test
    public void testLength() throws Exception {

        List tmp = new ArrayList(Arrays.asList("Hallo", "b", "c", 2));
        toTest.addAll(tmp);
        assertEquals("Error length self", 4, toTest.length());


    }


    @org.junit.Test
    public void testIsEmpty() throws Exception {

        toTest = new List_impl();
        out("SizeA: " + toTest.size());
        assertEquals("isEmpty with SelfList", true, toTest.isEmpty());
        toTest.add("Bingo");
        out("Size: " + toTest.size());
        assertEquals("isEmpty with SelfList", false, toTest.isEmpty());
    }

    @org.junit.Test
    public void testAdd()throws  Exception{
        toTest.add("Hallo");
        toTest.add("Du");
        toTest.add("Hier");

        assertEquals("Test Add Fehler", 3, toTest.length());

        System.out.println("========Add===========");
        System.out.println(""+ toTest);
        System.out.println("===================");
    }

    @org.junit.Test
    public void testAddAll() throws Exception{
        List<String> tmp = new ArrayList<String>(Arrays.asList("Hallo", "Du", "Hier", "Ist ja", "unglaublich"));
        toTest.addAll(tmp);

        assertEquals("Test Add All", 5, toTest.length());

        System.out.println("========AddAll===========");
        System.out.println(""+ toTest);
        System.out.println("===================");

    }

    @org.junit.Test
    public void testInsert() throws Exception {
        List<String> tmp = new ArrayList<String>(Arrays.asList("A", "B", "D"));


        toTest.addAll(tmp);
        List_inter result = new List_impl();
        result.add("A");
        result.add("B");
        result.add("C");
        result.add("D");


        //Self

        assertEquals("insert self", result.toString(), toTest.insert("C", 2).toString());


    }
    @org.junit.Test
    public void testHead() throws Exception {

        // With self List

        toTest.add("Hallo");
        toTest.add("Du");
        toTest.add("self");
        toTest.add("bingo");

        assertEquals("Head Self", "Hallo", ((Node_Impl) toTest.head()).getElem());
    }



    @Test
    public void testErstenExperiment(){
        List_inter tmp;
        Random random = new Random();
        int t;

        Mittelwert_Varianz_inter mittelwertVarianzInter = new Mittelwert_Varianz("Messreihe");
//
//            tmp = new List_impl();
//            t = random.nextInt(20)+1;
//
//            out("___> "+  t +" <____" + " Size: " + tmp.length());
//            for (int j = 0; j < t; j++){
//                tmp = tmp.cons((random.nextInt(Integer.MAX_VALUE) + 1));
//            }
//
//            out("Liste: "+tmp);
//            out("=====> Counter : " + tmp.getReferenzenCounter() + " Erwartet: " + (t+1) + " Size: " + tmp.length());
//
//            mittelwertVarianzInter.addMesswert(tmp.getReferenzenCounter());
//
//        err("Mittelwert: " + mittelwertVarianzInter.calculateMittelwert2() + " Varianz: " + mittelwertVarianzInter.calculateVarianz2());
//        out("===========================");
//        mittelwertVarianzInter.clearMessObj();
//            t = random.nextInt(20)+1;
//            tmp = new List_impl();
//            for (int i = 0; i < t; i++){
//                tmp.add((random.nextInt(20) + 1));
//            }
//
//            out("Liste: "+tmp);
//            out("=====> Counter : " + tmp.getReferenzenCounter() + " Erwartet: " + ((t*(t+1))/2) + " Size: " + tmp.length());
//        err("Mittelwert: " + mittelwertVarianzInter.calculateMittelwert2() + " Varianz: " + mittelwertVarianzInter.calculateVarianz2());

        out("===========================");
        Map<Integer, Double> resultMap = new HashMap<Integer,Double>();

        mittelwertVarianzInter.clearMessObj();
        for (int anzahlVersuche = 0; anzahlVersuche <10; anzahlVersuche++){

            t = random.nextInt(20)+1;
            tmp = new List_impl();
            for (int i = 1; i < t; i++){

                    tmp  =  tmp.insert(i,  random.nextInt(i));
            }

            out("Liste: " + tmp);
            out("=====> Counter : " + tmp.getReferenzenCounter() + " Erwartet: " + ((t*(t+1))/2) + " Size: " + tmp.size());
            mittelwertVarianzInter.addMesswert(tmp.getReferenzenCounter());

            resultMap.put(anzahlVersuche, mittelwertVarianzInter.calculateMittelwert2());


        }
            err("========>" + resultMap);


    }

    private void out(String str){
        System.out.println(str);
    }

    private void err(String str){
        System.err.println(str);
    }

}
